package com.ruoyi.system.controller;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.excel.ProvidePlan;
import com.ruoyi.system.service.*;
import com.ruoyi.system.util.MapperUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/system/ProviderConfig")
public class InvoiceProviderConfigController extends BaseController
{
    @Autowired
    private IInvoiceProviderConfigService invoiceProviderConfigService;
    @Autowired
    private IInvoiceRecordsService invoiceRecordsService;
    @Autowired
    private IInvoiceServiceDetailService iInvoiceServiceDetailService;
    @Autowired
    private IProvideRecordsService iProvideRecordsService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(InvoiceProviderConfig invoiceProviderConfig)
    {
        startPage();
        List<InvoiceProviderConfig> list = invoiceProviderConfigService.selectInvoiceProviderConfigList(invoiceProviderConfig);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvoiceProviderConfig invoiceProviderConfig)
    {
        List<InvoiceProviderConfig> list = invoiceProviderConfigService.selectInvoiceProviderConfigList(invoiceProviderConfig);
        ExcelUtil<InvoiceProviderConfig> util = new ExcelUtil<InvoiceProviderConfig>(InvoiceProviderConfig.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invoiceProviderConfigService.selectInvoiceProviderConfigById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvoiceProviderConfig invoiceProviderConfig)
    {
        return toAjax(invoiceProviderConfigService.insertInvoiceProviderConfig(invoiceProviderConfig));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvoiceProviderConfig invoiceProviderConfig)
    {
        return toAjax(invoiceProviderConfigService.updateInvoiceProviderConfig(invoiceProviderConfig));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invoiceProviderConfigService.deleteInvoiceProviderConfigByIds(ids));
    }

    @Anonymous
    @RequestMapping("/providerServiceSearch")
    public String providerServiceSearch(@RequestParam(value = "startTime",required = true)String startTime)
    {
        HashMap<String,Object> result = new HashMap<>();
        InvoiceProviderConfig invoiceProviderConfig = new InvoiceProviderConfig();
        List<Object> providerList = new ArrayList<>();

        try{
            List<InvoiceProviderConfig> providerConfig = invoiceProviderConfigService.selectInvoiceProviderConfigList(invoiceProviderConfig);
            for(InvoiceProviderConfig provider:providerConfig)
            {
                Double zxkcb = 0.0;
                Double zxksr = 0.0;
                Double ptcb = 0.0;
                Double ptsr = 0.0;
                Double zxkfwf = 0.0;
                Double ptfwf = 0.0;
                Double zxklr = 0.0;
                Double ptlr = 0.0;
                HashMap<String,Object> providerMap = new HashMap<>();
                //中心库进价
                Double zxkPlanMoney = invoiceRecordsService.selectAllMoneyPrice(provider.getProvider(), startTime);
                if(zxkPlanMoney==null)
                {
                    providerMap.put("zxkcb",0);
                }
                else
                {
                    providerMap.put("zxkcb",String.format("%.2f",zxkPlanMoney));
                    zxkcb = zxkPlanMoney;
                }
                //中心库售价
                Double zxkAllMoney = invoiceRecordsService.selectMoney(provider.getProvider(), startTime);
                if(zxkAllMoney==null)
                {
                    providerMap.put("zxksr",0);
                }
                else
                {
                    providerMap.put("zxksr",String.format("%.2f",zxkAllMoney));
                    zxksr = zxkAllMoney;
                }
                //平台进价
                Double ptPlanMoney = iProvideRecordsService.selectAllMoneyPrice(provider.getProvider(), startTime);
                if(ptPlanMoney==null)
                {
                    providerMap.put("ptcb",0);
                }
                else
                {
                    providerMap.put("ptcb",String.format("%.2f",ptPlanMoney));
                    ptcb = ptPlanMoney;
                }
                //平台售价
                Double ptAllMoney = iProvideRecordsService.selectMoney(provider.getProvider(), startTime);
                if(ptAllMoney==null)
                {
                    providerMap.put("ptsr",0);
                }
                else
                {
                    providerMap.put("ptsr",String.format("%.2f",ptAllMoney));
                    ptsr = ptAllMoney;
                }
                //中心库服务费
                Double zxkServiceMoney = iInvoiceServiceDetailService.selectMoney(provider.getProvider(), startTime);
                if(zxkServiceMoney==null||zxkServiceMoney==0)
                {
                    providerMap.put("zxkfwf",0);
                }
                else
                {
                    providerMap.put("zxkfwf",String.format("%.2f",zxkServiceMoney));
                    zxkfwf = zxkServiceMoney;
                }
                //平台服务费
                Double ptServiceMoney = iInvoiceServiceDetailService.selectAllMoneyPrice(provider.getProvider(), startTime);
                if(ptServiceMoney==null||ptServiceMoney==0)
                {
                    providerMap.put("ptfwf",0);
                }
                else
                {
                    providerMap.put("ptfwf",String.format("%.2f",ptServiceMoney));
                    ptfwf = ptServiceMoney;
                }
                zxklr = zxksr-zxkcb;
                ptlr = ptsr-ptcb;
                providerMap.put("供应商",provider.getProvider());
                providerMap.put("providerName",provider.getProvider());
                providerMap.put("zxklr",String.format("%.2f",(zxklr)));
                providerMap.put("ptlr",String.format("%.2f",(ptlr)));
                providerMap.put("zlr",String.format("%.2f",(zxklr+ptlr)));
                providerMap.put("zfwf",String.format("%.2f",(zxkfwf+ptfwf)));
                providerList.add(providerMap);

            }

            /*// 对列表进行分页处理
            int total = orders.size();
            int startIndex = (pageNum - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, total);
            List<Order> paginatedList = orders.subList(startIndex, endIndex);
            // 返回分页结果
            result.put("list", paginatedList);
            result.put("total", total);
            result.put("pageNum", pageNum);
            result.put("pageSize", pageSize);*/
            result.put("code",200);
            result.put("msg","查询成功！");
            result.put("providerList",providerList);
        }catch (Exception e){
            result.put("code",500);
            result.put("msg",e);
        }
        return MapperUtils.mapToJson(result);
    }

    /**
     * 每月供应商服务费计算
     */
    @Anonymous
    @RequestMapping("/addProviderService")
    @Transactional(rollbackFor = Exception.class)
    public String addProviderService(@RequestParam(value = "startTime",required = true)String startTime)
    {
        HashMap<String,Object> returnMap = new HashMap<>();
        InvoiceProviderConfig invoiceProviderConfig = new InvoiceProviderConfig();
        try{
            List<InvoiceProviderConfig> providerConfig = invoiceProviderConfigService.selectInvoiceProviderConfigList(invoiceProviderConfig);
            for(InvoiceProviderConfig provider:providerConfig)
            {
                //供应商服务费计算%
                if(Objects.equals(provider.getStatus(), "1") && Objects.equals(provider.getIsPcs(), "0") && Objects.equals(provider.getIsSpecial(), "0"))
                {
                    zxkForMoney(provider,startTime);

                    warehouseForMoney(provider,startTime);
                }
                //供应商服务费计算（根据条数）
                else if (Objects.equals(provider.getStatus(), "1") && Objects.equals(provider.getIsPcs(), "1") && Objects.equals(provider.getIsSpecial(), "0"))
                {
                    zxkForPcs(provider,startTime);

                    warehouseForPcs(provider,startTime);
                }
                //特殊供应商特殊产品处理
                else if (Objects.equals(provider.getStatus(), "1") && Objects.equals(provider.getIsSpecial(), "1"))
                {
                    if(Objects.equals(provider.getProvider(), "上海宇申"))
                    {
                        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
                        double ys3 = ysForMoney3(provider.getProvider(),startTime);
                        double ys10 = ysForMoney10(provider.getProvider(),startTime);

                        invoiceServiceDetail.setProvider(provider.getProvider());
                        invoiceServiceDetail.setInvoiceTime(startTime);
                        invoiceServiceDetail.setStatus("1");
                        invoiceServiceDetail.setServiceCharge(String.format("%.2f",ys3+ys10));
                        invoiceServiceDetail.setCompany("供应商");
                        invoiceServiceDetail.setProvideWare("");
                        iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
                    }
                    else if (Objects.equals(provider.getProvider(), "上海盛丹"))
                    {
                        sdForMoney(provider.getProvider(),startTime);
                        sdForMoneyForpt(provider.getProvider(),startTime);
                    }
                    else if (Objects.equals(provider.getProvider(), "祥皓贸易"))
                    {
                        xhForMoneyZXK(provider.getProvider(),startTime);
                        xhForMoney(provider.getProvider(),startTime);
                    }
                    else if(Objects.equals(provider.getProvider(), "上海豫东"))
                    {
                        ydForMoney(provider.getProvider(),startTime);
                    }
                    else if(Objects.equals(provider.getProvider(), "上海豪贤"))
                    {
                        hxForMoney(provider.getProvider(),startTime);
                    }
                    else if(Objects.equals(provider.getProvider(), "上海帕尔茨"))
                    {
                        prcForMoney(provider.getProvider(),startTime);
                    }
                    else if(Objects.equals(provider.getProvider(), "鼎厉"))
                    {
                        dlForMoney(provider.getProvider(),startTime);
                    }
                    else if(Objects.equals(provider.getProvider(), "上海友瑗"))
                    {
                        yyForMoney(provider.getProvider(),startTime);
                    }
                    else if(Objects.equals(provider.getProvider(), "上海永售"))
                    {
                        ysForMoney(provider.getProvider(),startTime);
                        ysForMoneyForpt(provider.getProvider(),startTime);

                    }
                    else if(Objects.equals(provider.getProvider(), "上海盛丹"))
                    {
                        sdForMoney(provider.getProvider(),startTime);
                        sdForMoneyForpt(provider.getProvider(),startTime);
                    }
                }
            }

            returnMap.put("code",200);
            returnMap.put("msg","服务费计算完成");
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }

    /**
     * 非中心库供应商服务费
     */
    public int warehouseForMoney(InvoiceProviderConfig provider,String startTime)
    {
        //获取当月供应商开票金额，开票金额*系数（百分比%）=服务费
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectAllMoneyPrice(provider.getProvider(), startTime);
        Double charge=0.00;
        int count = 0;
        if(AllMoney==null)
        {

        }
        else
        {
            Double coefficient = Double.parseDouble(provider.getCoefficient())/100;
            charge = coefficient*AllMoney;

            invoiceServiceDetail.setProvider(provider.getProvider());
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("供应商");
            invoiceServiceDetail.setProvideWare("");

            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }


        return count;
    }

    /**
     * 非中心库供应商服务费(条数)
     */
    public int warehouseForPcs(InvoiceProviderConfig provider,String startTime)
    {
        //获取当月供应商开票金额，开票金额*系数（百分比%）=服务费
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double zxkAllCount = iProvideRecordsService.selectAllMoneyCount(provider.getProvider(), startTime);
        Double zxkCharge=0.00;
        int count = 0;
        if(zxkAllCount==null)
        {

        }
        else
        {
            Double coefficient = Double.parseDouble(provider.getCoefficient());
            zxkCharge = coefficient*zxkAllCount;

            invoiceServiceDetail.setProvider(provider.getProvider());
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",zxkCharge));
            invoiceServiceDetail.setCompany("供应商");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }

        return count;
    }

    /**
     * 中心库供应商服务费（条数）
     */
    public int zxkForPcs(InvoiceProviderConfig provider,String startTime)
    {
        //获取当月供应商领用数量，领用条数*系数（每条金额）=服务费
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllCount = invoiceRecordsService.selectAllCount(provider.getProvider(), startTime);
        Double charge=0.00;
        int count=0;
        if(AllCount==null)
        {

        }
        else
        {
            Double coefficient = Double.parseDouble(provider.getCoefficient());
            charge = coefficient*AllCount;

            invoiceServiceDetail.setProvider(provider.getProvider());
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }

        return count;
    }

    /**
     * 中心库供应商服务费
     */
    public int zxkForMoney(InvoiceProviderConfig provider,String startTime)
    {
        //获取当月供应商开票金额，开票金额*系数（百分比%）=服务费
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double zxkAllMoney = invoiceRecordsService.selectAllMoneyPrice(provider.getProvider(), startTime);
        Double zxkCharge=0.00;
        int count = 0;
        if(zxkAllMoney==null)
        {

        }
        else
        {
            Double coefficient = Double.parseDouble(provider.getCoefficient())/100;
            zxkCharge = coefficient*zxkAllMoney;

            invoiceServiceDetail.setProvider(provider.getProvider());
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",zxkCharge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }

        return count;
    }

    /**
     * 宇申3%
     */
    public double ysForMoney3(String provider,String startTime)
    {
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectYSMoneyPrice3(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 0.03;
            charge = coefficient*AllMoney;
        }
        return charge;
    }

    /**
     * 宇申10%
     */
    public double ysForMoney10(String provider,String startTime)
    {
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectYSMoneyPrice10(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 0.1;
            charge = coefficient*AllMoney;
        }
        return charge;
    }

    /**
     * 盛丹zxk
     */
    public int sdForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectSDMoneyPrice(startTime);
        Double LT = iProvideRecordsService.selectSDMoneyPriceLT(startTime);
        Double RHY = iProvideRecordsService.selectSDMoneyPriceRHY(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        Double RHYCoefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            if(LT==null)
            {
                LT=0.00;
            }
            if(RHY==null)
            {
                RHY=0.00;
            }
            coefficient = 0.1;
            RHYCoefficient = 0.07;
            charge = coefficient*AllMoney;
            charge = charge + LT;
            charge = charge + RHY*RHYCoefficient;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 盛丹pt
     */
    public int sdForMoneyForpt(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectSDMoneyPricePt(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 0.1;
            charge = coefficient*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("供应商");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 永售zxk
     */
    public int ysForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectYSMoneyPrice(startTime);
        Double RHY = iProvideRecordsService.selectYSMoneyPriceRHY(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        Double RHYCoefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            if(RHY==null)
            {
                RHY=0.00;
            }
            coefficient = 0.1;
            RHYCoefficient = 0.07;
            charge = coefficient*AllMoney;
            charge = charge + RHY*RHYCoefficient;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 永售pt
     */
    public int ysForMoneyForpt(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectYSMoneyPricePt(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 0.1;
            charge = coefficient*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("供应商");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 祥皓贸易ZXK
     */
    public int xhForMoneyZXK(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectXHMoneyPriceZXK(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 0.1;
            charge = coefficient*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 祥皓贸易平台
     */
    public int xhForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectXHMoneyPrice(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 0.1;
            charge = coefficient*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("供应商");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 豫东中心库
     */
    public int ydForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        Double AllMoney = iProvideRecordsService.selectYDMoneyPrice(startTime);
        Double charge=0.00;
        Double coefficient = 0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            coefficient = 15.00;
            charge = coefficient*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 豪贤中心库
     */
    public int hxForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        //Double AllMoney = iProvideRecordsService.selectHXMoneyPrice(startTime);
        Double AllMoney = 0.07;
        Double RHY = iProvideRecordsService.selectHXMoneyRHY(startTime);
        Double charge=0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            if(RHY==null)
            {
                RHY=0.00;
            }
            charge = RHY*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 帕尔茨中心库
     */
    public int prcForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        //Double AllMoney = iProvideRecordsService.selectHXMoneyPrice(startTime);
        Double AllMoney = 0.07;
        Double RHY = iProvideRecordsService.selectPRCMoneyRHY(startTime);
        Double charge=0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            if(RHY==null)
            {
                RHY=0.00;
            }
            charge = RHY*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 鼎厉中心库
     */
    public int dlForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        //Double AllMoney = iProvideRecordsService.selectHXMoneyPrice(startTime);
        Double AllMoney = 0.07;
        Double RHY = iProvideRecordsService.selectDLMoneyRHY(startTime);
        Double charge=0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            if(RHY==null)
            {
                RHY=0.00;
            }
            charge = RHY*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    /**
     * 友瑗中心库
     */
    public int yyForMoney(String provider,String startTime)
    {
        int count = 0;
        InvoiceServiceDetail invoiceServiceDetail = new InvoiceServiceDetail();
        //Double AllMoney = iProvideRecordsService.selectHXMoneyPrice(startTime);
        Double AllMoney = 0.07;
        Double RHY = iProvideRecordsService.selectYYMoneyRHY(startTime);
        Double charge=0.00;
        if(AllMoney==null)
        {

        }
        else
        {
            if(RHY==null)
            {
                RHY=0.00;
            }
            charge = RHY*AllMoney;

            invoiceServiceDetail.setProvider(provider);
            invoiceServiceDetail.setInvoiceTime(startTime);
            invoiceServiceDetail.setStatus("1");
            invoiceServiceDetail.setServiceCharge(String.format("%.2f",charge));
            invoiceServiceDetail.setCompany("中心库");
            invoiceServiceDetail.setProvideWare("");
            count = iInvoiceServiceDetailService.insertInvoiceServiceDetail(invoiceServiceDetail);
        }
        return count;
    }

    @Anonymous
    @RequestMapping("/export")
    public void export(HttpServletResponse response,@RequestParam(value = "startTime",required = true)String startTime) throws Exception
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        StringBuffer fileNameBuffer = new StringBuffer("");
        fileNameBuffer.append("providerPlan").append("-").append(sdf.format(new Date())).append(".xls");
        String filename = URLEncoder.encode(fileNameBuffer.toString(), "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + filename );

        HashMap<String,Object> result = new HashMap<>();
        InvoiceProviderConfig invoiceProviderConfig = new InvoiceProviderConfig();
        List<ProvidePlan> providerList = new ArrayList<>();
        try{
            List<InvoiceProviderConfig> providerConfig = invoiceProviderConfigService.selectInvoiceProviderConfigList(invoiceProviderConfig);
            for(InvoiceProviderConfig provider:providerConfig)
            {
                Double zxkcb = 0.0;
                Double zxksr = 0.0;
                Double ptcb = 0.0;
                Double ptsr = 0.0;
                Double zxkfwf = 0.0;
                Double ptfwf = 0.0;
                Double zxklr = 0.0;
                Double ptlr = 0.0;
                ProvidePlan providerMap = new ProvidePlan();
                //中心库进价
                if(Objects.equals(provider.getProvider(), "上海富阳")||Objects.equals(provider.getProvider(), "上海来兴")||Objects.equals(provider.getProvider(), "上海安民"))
                {
                    Double zxkPlanMoney = invoiceRecordsService.selectAllMoneyPriceForAudit(provider.getProvider(), startTime);
                    if(zxkPlanMoney==null)
                    {
                        providerMap.setZxkcb(0.0);
                    }
                    else
                    {
                        providerMap.setZxkcb(Double.valueOf(String.format("%.2f",zxkPlanMoney)));
                        zxkcb = zxkPlanMoney;
                    }
                }
                else
                {
                    Double zxkPlanMoney = invoiceRecordsService.selectAllMoneyPrice(provider.getProvider(), startTime);
                    if(zxkPlanMoney==null)
                    {
                        providerMap.setZxkcb(0.0);
                    }
                    else
                    {
                        providerMap.setZxkcb(Double.valueOf(String.format("%.2f",zxkPlanMoney)));
                        zxkcb = zxkPlanMoney;
                    }
                }
                //中心库售价
                if(Objects.equals(provider.getProvider(), "上海富阳")||Objects.equals(provider.getProvider(), "上海来兴")||Objects.equals(provider.getProvider(), "上海安民"))
                {
                    Double zxkAllMoney = invoiceRecordsService.selectMoneyPriceForAudit(provider.getProvider(), startTime);
                    if(zxkAllMoney==null)
                    {
                        providerMap.setZxksr(0.0);
                    }
                    else
                    {
                        providerMap.setZxksr(Double.valueOf(String.format("%.2f",zxkAllMoney)));
                        zxksr = zxkAllMoney;
                    }
                }
                else
                {
                    Double zxkAllMoney = invoiceRecordsService.selectMoney(provider.getProvider(), startTime);
                    if(zxkAllMoney==null)
                    {
                        providerMap.setZxksr(0.0);
                    }
                    else
                    {
                        providerMap.setZxksr(Double.valueOf(String.format("%.2f",zxkAllMoney)));
                        zxksr = zxkAllMoney;
                    }
                }

                //平台进价
                Double ptPlanMoney = iProvideRecordsService.selectAllMoneyPrice(provider.getProvider(), startTime);
                if(ptPlanMoney==null)
                {
                    providerMap.setPtcb(0.0);
                }
                else
                {
                    providerMap.setPtcb(Double.valueOf(String.format("%.2f",ptPlanMoney)));
                    ptcb = ptPlanMoney;
                }
                //平台售价
                Double ptAllMoney = iProvideRecordsService.selectMoney(provider.getProvider(), startTime);
                if(ptAllMoney==null)
                {
                    providerMap.setPtsr(0.0);
                }
                else
                {
                    providerMap.setPtsr(Double.valueOf(String.format("%.2f",ptAllMoney)));
                    ptsr = ptAllMoney;
                }
                //中心库服务费
                Double zxkServiceMoney = iInvoiceServiceDetailService.selectMoney(provider.getProvider(), startTime);
                if(zxkServiceMoney==null)
                {
                    providerMap.setZxkfwf(0.0);
                }
                else
                {
                    providerMap.setZxkfwf(Double.valueOf(String.format("%.2f",zxkServiceMoney)));
                    zxkfwf = zxkServiceMoney;
                }
                //平台服务费
                Double ptServiceMoney = iInvoiceServiceDetailService.selectAllMoneyPrice(provider.getProvider(), startTime);
                if(ptServiceMoney==null)
                {
                    providerMap.setPtfwf(0.0);
                }
                else
                {
                    providerMap.setPtfwf(Double.valueOf(String.format("%.2f",ptServiceMoney)));
                    ptfwf = ptServiceMoney;
                }
                zxklr = zxksr-zxkcb;
                ptlr = ptsr-ptcb;
                providerMap.setProviderName(provider.getProvider());
                providerMap.setZxklr(Double.valueOf(String.format("%.2f",(zxklr))));
                providerMap.setPtlr(Double.valueOf(String.format("%.2f",(ptlr))));
                providerMap.setZlr(Double.valueOf(String.format("%.2f",(zxklr+ptlr))));
                providerMap.setZfwf(Double.valueOf(String.format("%.2f",(zxkfwf+ptfwf))));
                providerList.add(providerMap);
            }

        EasyExcel.write(response.getOutputStream(), ProvidePlan.class).sheet("配件利润表").doWrite(providerList);
    }catch (Exception e){
            result.put("code",500);
            result.put("msg","服务器异常 请稍后再试！");
        }

}}
